package com.dr.web.controllers;

import com.dr.common.pojo.RepMess;
import com.dr.common.ution.StringUtil;
import com.dr.common.ution.VerifyCodeUtils;
import com.dr.exception.ServiceExcep;
import com.dr.pojo.SysUser;
import com.dr.service.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by tugou on 2017/5/8.
 */
@Controller
@RequestMapping("admin")
public class AdminLoginController {
    @Autowired
    SysLoginService sysLoginService;

    @RequestMapping(value = "sysLogin", method = RequestMethod.GET)
    public String login(){
        return "background/login";
    }

    @RequestMapping(value = "loginOut", method = RequestMethod.GET)
    public String loginOut(HttpSession session){
        session.removeAttribute("sysuser");
        return "background/login";
    }

    @RequestMapping(value = "getverifyCode", method = RequestMethod.GET)
    public void getverifyCode(HttpServletResponse response, HttpSession session){
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //存入会话session
        session.removeAttribute("verifyCode");
        session.setAttribute("verifyCode",verifyCode.toLowerCase());//验证码统一转成小写
        int w = 100, h = 30;
        try {
            VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "sysLogin",method = RequestMethod.POST)
    public String login(SysUser user,HttpSession session,String verifyCode,Model model){
        RepMess msg = new RepMess();
        verifyCode=verifyCode.toLowerCase();
        String code = (String)session.getAttribute("verifyCode");
        if(StringUtil.isEmpty((String) session.getAttribute("verifyCode")))
            return "redirect:/admin/sysLogin";
        if(StringUtil.isEmpty(user.getLoginName()))
            msg.setErrmsg("登录名为空");
        if(StringUtil.isEmpty(user.getUserPwd()))
            msg.setErrmsg("密码名为空");
        if(StringUtil.isEmpty(verifyCode))
            msg.setErrmsg("验证码为空");
        if(!verifyCode.equals(code)){
            msg.setErrmsg("验证码错误，请重新输入");
        }
        try {
            SysUser sysUser = sysLoginService.getSysUserByLoginInfo(user);
            session.setAttribute("sysuser",sysUser);
        } catch (ServiceExcep excep) {
            msg.setErrmsg(excep.getMsg());
            model.addAttribute("msg",msg);
            return "background/login";
        }
        model.addAttribute("msg",msg);
        if(StringUtil.isEmpty(msg.getErrmsg()))
            return "redirect:/sys/getStudentList";
        else
            return "background/login";
    }
}
